
#function to graph treatment effects with CIs, Study 1 only
plot_estCIs_S1 <- function(fit, plotname, DVlabel, condition_idx=NULL){
  
  est_CI <- as.data.frame(cbind(fit$coefficients, confint(fit))[2:5,])
  colnames(est_CI) <- c("est", "CI_lo", "CI_hi")
  est_CI$condition <- gsub("disc_treatment.7", "", rownames(est_CI))
  est_CI$condition <- factor(est_CI$condition, levels=treatment_lvls)
  
  #add estimate labels w/o leading zeroes
  est_CI$est_label <- sprintf("%.2f", est_CI$est)
  est_CI$est_label <- gsub("^0", "", est_CI$est_label)
  print(est_CI)
  
  pdf(paste0("figures/", plotname, ".pdf"), 3, 4)
  print(
    ggplot(est_CI, aes(x=condition, y=est)) + 
      geom_hline(yintercept=0, color="dark grey") +
      geom_errorbar(aes(ymin=CI_lo, ymax=CI_hi), width=.1) +
      geom_point() +
      ylab(paste0("Effect on ", DVlabel)) +
      scale_x_discrete(labels = treatment_labels) +
      theme_bw() +
      theme(axis.title.x=element_blank(),
            axis.title.y=element_text(size=11),
            axis.text=element_text(size=11),
            axis.text.x = element_text(angle = 45, vjust = 1, hjust=1)
      ) +
      geom_text(aes(label=est_label, hjust=-.3, vjust=-.05))
  )
  dev.off()
  
}

#function to graph treatment effects with CIs
plot_estCIs <- function(fit, plotname, DVlabel, treatment_name, treatment_lvls, treatment_labels=treatment_lvls,
                        title=NULL, ymax=NULL, w=7.5, h=4.5){
  
  condition_names <- paste0(treatment_name, treatment_lvls)
  est_CI <- as.data.frame(cbind(fit$coefficients, confint(fit))[condition_names,])
  colnames(est_CI) <- c("est", "CI_lo", "CI_hi")
  est_CI$condition <- gsub(treatment_name, "", rownames(est_CI))
  est_CI$condition <- factor(est_CI$condition, levels=treatment_lvls)
  levels(est_CI$condition) <- treatment_labels
  
  #add estimate labels w/o leading zeroes
  est_CI$est_label <- sprintf("%.2f", est_CI$est)
  est_CI$est_label <- gsub("^0", "", est_CI$est_label)
  est_CI$est_label <- gsub("^-0", "-", est_CI$est_label)
  print(est_CI)
  
  pdf(paste0("figures/", plotname, ".pdf"), w, h)
  print(ggplot(est_CI, aes(x=condition, y=est)) +
          geom_hline(yintercept=0, color="dark grey") +
          geom_errorbar(aes(ymin=CI_lo, ymax=CI_hi), width=.1) +
          geom_point() +
          {if(!is.null(ymax)) ylim(0, ymax)} +
          {if(!is.null(title)) ggtitle(title)} +
          ylab(paste0("Effect on ", DVlabel)) +
          theme_bw() +
          theme(axis.title.x=element_blank(),
                axis.title.y=element_text(size=14),
                axis.text=element_text(size=14),
                plot.title=element_text(size=16, face="bold")) +
          geom_text(aes(label=est_label, hjust=-.35, vjust=-.05), size=5)
  )
  dev.off()
  
}

